<template>
    <section class="container">
        <el-row>
            <el-col :span="24">
                <el-checkbox
                    v-model="checkAll"
                    @change="selectAll"
                    class="checkbox"
                >全选
                </el-checkbox>
            </el-col>
            <el-col :span="24">
                <el-card
                    class="box-card mb10"
                    v-for="(item, itemIndex) in workflowTree"
                    :key="itemIndex"
                >
                    <div slot="header" class="clearfix">
                        <span class="fwb">{{ item.name }} ({{ item.list.length }})</span>
                    </div>
                    <el-row :gutter="10">
                        <el-col
                            :key="index"
                            :span="6"
                            v-for="(child, index) in item.list"
                            style="height: 74px;width: 250px;"
                            @click.native="handleFlow(child)"
                        >
                            <div class="item_tree" :class="{ 'item_tree__active': child.check }">
                                <div class="tree_item_icon">
                                    <i
                                        class="btn_cls"
                                        :style="{ color: child.check ? '#FFF' : child.icoColor }"
                                        :class="child.icoName"
                                    ></i>
                                </div>
                                <div class="tree_item_content">
                                    <el-tooltip
                                        :disabled="child.name.length <= 10"
                                        :content="child.name"
                                        placement="top"
                                    >
                                        <div>{{ child.name }}</div>
                                    </el-tooltip>
                                </div>
                                <i
                                    style="float:right;display: inline-block;margin-top: 16px;margin-right: 20px"
                                    :class="{ 'check-style-unequal-width1': child.check }"
                                >
                                </i>
                            </div>
                        </el-col>
                    </el-row>
                </el-card>
            </el-col>
        </el-row>
    </section>
</template>
<script>
    export default {
        name: "distributionWorkflow",
        props: ["roleId"],
        mounted() {
            this.getWorkflowTreeF();
        },
        data() {
            return {
                workflowTree: [],
                workflowIds: [],
                checkAll: false
            };
        },
        watch: {
            roleId(val) {
                val && this.getWorkflowTreeF();
            },
            workflowTree: {
                handler(val) {
                    let arr = [];
                    val.map(item => {
                        item.list.map(item1 => {
                            arr.push(item1.check);
                        });
                        this.checkAll = !arr.includes(false);
                    });
                },
                deep: true
            }
        },
        methods: {
            //全选
            selectAll(check) {
                this.workflowTree.map(item => {
                    item.list.map(child => {
                        child.check = check;
                        if (check) {
                            let index = this.workflowIds.findIndex(
                                flowId => flowId === child.id
                            );
                            index === -1 && this.workflowIds.push(child.id);
                        } else {
                            this.workflowIds = [];
                        }
                    });
                });
            },
            //回显数据
            async getCheck() {
                this.workflowIds = [];
                let tem = {roleId: this.roleId};
                await this.API.getRoleWorkflowByRoleId(tem).then(async ret => {
                    this.workflowIds = ret.data;
                });
            },
            //获取流程树形结构
            async getWorkflowTreeF() {
                await this.getCheck();
                this.API.getWorkflowTree({allData: true}).then(res => {
                    this.workflowTree = res.data;
                    this.workflowTree.map(item => {
                        if (item.list) {
                            item.list.map(child => {
                                this.$set(
                                    child,
                                    "check",
                                    this.workflowIds.findIndex(args => args == child.id) != -1
                                );
                            });
                            this.checkAll =
                                item.list.findIndex(index => index.check === false) === -1;
                        }
                    });
                });
            },
            //点击流程
            handleFlow(child) {
                child.check = !child.check;
                if (child.check) {
                    this.workflowIds.push(child.id);
                } else {
                    let index = this.workflowIds.findIndex(item => item === child.id);
                    this.workflowIds.splice(index, 1);
                }
            }
        }
    };
</script>
<style scoped lang="scss">
    $normal_color: #f4faff;
    $active_color: #5fe1e0;

    .container {
        .checkbox {
            margin: 0 0 20px 20px
        }

        .item_tree {
            background: $normal_color;
            border-radius: 5px;
            margin-bottom: 10px;
            padding-left: 10px;
            cursor: pointer;
            height: 64px;


            &__active {
                background: $active_color;

                div {
                    color: white;
                }
            }

            .tree_item {

                &_icon {
                    float: left;
                    padding-top: 20px;
                    margin: 0 8px;

                    .btn_cls {
                        font-size: 23px !important;
                        margin-right: 2px;
                    }
                }

                &_content {
                    float: left;
                    padding-top: 21px;
                    width: 65%;

                    div {
                        overflow: hidden;
                        white-space: nowrap;
                        text-overflow: ellipsis;
                    }
                }
            }
        }
    }
</style>
